package com.idanchuang.count.core.model;

import com.idanchuang.component.base.exception.core.ExFactory;
import com.idanchuang.count.IErrorCode;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

/**
 * @author Richard_yyf
 * @version 1.0 2021/5/28
 */
@Data
@Component
@ConfigurationProperties(prefix = "count.biz")
public class BizProperties {

    /**
     * 每个接入应用的 oss 配置
     */
    private Map<String, BizConfig> config;


    public Integer getBizId(String bizCode) {
        return Optional
                .ofNullable(config.get(bizCode))
                .orElseThrow(() -> ExFactory.throwWith(IErrorCode.CONFIG_NOT_FOUND, bizCode))
                .getId();
    }

    public BizConfig getSingleConfig(String bizCode) {
        return Optional
                .ofNullable(config.get(bizCode))
                .orElseThrow(() -> ExFactory.throwWith(IErrorCode.CONFIG_NOT_FOUND, bizCode))
                ;
    }

    public BizConfig getSingleConfigNotThrowEx(String bizCode) {
        return config.get(bizCode);
    }
}
